package com.hrt.datasender.datacollect.drive.modbustcpflow;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.btcode.common.BitType;
import com.hrt.datasender.datacollect.drive.DriveConfigModel;
import com.hrt.datasender.datacollect.drive.DriveParameterMetadata;
import com.hrt.datasender.datacollect.drive.modbus.tcp.NetBaseDrive;
import com.hrt.datasender.datadao.model.BcDeviceConfigModel;
import com.hrt.datasender.datadao.model.BcDeviceMonitorParamModel;
import com.hrt.datasender.datadao.model.BcDevicesModel;
import com.hrt.datasender.datadao.model.BdSourceModel;
import com.hrt.serial.modbus.ModbusDataDecodeTCP;
import com.hrt.serial.modbus.ModbusDataModel;

/**
 * modbusTCPFlow驱动
 * 
 * @author 张文成
 *
 */
public class ModbusTCPFlowDrive extends NetBaseDrive {
    private final int flowTrans = 3600;
    
    protected ModbusDataDecodeTCP decode = new ModbusDataDecodeTCP();

    @Override
    public List<BdSourceModel> processNetExportData(byte[] data, HashMap<Integer, String> paramCodeMap,
            DriveConfigModel deviceConfigs) {

        List<ModbusDataModel> modbusDataModels = decode.dataModelDecode(data, data.length, deviceConfigs);
        ModbusDataModel modbusDataModel = modbusDataModels.get(0);
        double[] doubleData = modbusDataModel.getData();

        HashMap<String, String> configs = deviceConfigs.getConfig();
        Integer flowIns1 = Integer.parseInt(configs.get("flowIns1"));
        Integer flowAcc1 = Integer.parseInt(configs.get("flowAcc1"));
        Integer flowIns2 = Integer.parseInt(configs.get("flowIns2"));
        Integer flowAcc2 = Integer.parseInt(configs.get("flowAcc2"));
        String flowInsUnitCon = configs.get("flowInsUnitCon");
        Double flowIns = 0.0;
        Double flowAcc = 0.0;
        flowIns = (doubleData[flowIns1] + doubleData[flowIns2]) * flowTrans;
        flowAcc = doubleData[flowAcc1] + doubleData[flowAcc2];

        if ("1".equals(flowInsUnitCon)) {
            flowIns = flowIns * 3.6;
        }
        else if ("2".equals(flowInsUnitCon)) {
            flowIns = flowIns / 3.6;
        }

        List<BdSourceModel> sourceModels = new ArrayList<>();
        BdSourceModel sourModelIns = new BdSourceModel();
        sourModelIns.setParamCodeBus("B01");
        sourModelIns.setDatatime(new Date());
        sourModelIns.setMeasRe(flowIns);
        sourceModels.add(sourModelIns);

        BdSourceModel sourModelAcc = new BdSourceModel();
        sourModelAcc.setParamCodeBus("B01TOTAL");
        sourModelAcc.setDatatime(new Date());
        sourModelAcc.setMeasRe(flowAcc);
        sourceModels.add(sourModelAcc);
        return sourceModels;
    }

    @Override
    public List<DriveParameterMetadata> getParameterMetadata() {
        List<DriveParameterMetadata> params = new ArrayList<>();
        params.add(new DriveParameterMetadata("ip", "IP"));
        params.add(new DriveParameterMetadata("port", "端口"));
        params.add(new DriveParameterMetadata("slaveNo", "从站编号"));
        params.add(new DriveParameterMetadata("startNo", "起始位置"));
        params.add(new DriveParameterMetadata("dataLength", "数据长度"));
        params.add(new DriveParameterMetadata("driveInstanceId", "驱动实例ID"));
        params.add(new DriveParameterMetadata("flowIns1", "流量计1瞬时数据位编号"));
        params.add(new DriveParameterMetadata("flowAcc1", "流量计1累积数据位编号"));
        params.add(new DriveParameterMetadata("flowIns2", "流量计2瞬时数据位编号"));
        params.add(new DriveParameterMetadata("flowAcc2", "流量计2累积数据位编号"));
        params.add(new DriveParameterMetadata("flowInsUnitCon", "瞬时流量单位换算（0：不变 1：乘3.6 2：除3.6）"));
        params.add(new DriveParameterMetadata("functionCode", "功能码"));
        return params;
    }

    @Override
    public String getConsStr() {
        return "0/10 * * * * ?";
    }

    @Override
    public String getDriveName() {
        return "ModbusTCPFlow驱动";
    }

    @Override
    public String getNote() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void beforeInit(BcDevicesModel device, List<BcDeviceMonitorParamModel> params,
            List<BcDeviceConfigModel> configs) {
        // TODO Auto-generated method stub

    }
}
